﻿using Microsoft.AspNetCore.Mvc;
using OpenCvSharp;
using Sdcb.PaddleOCR;
using Sdcb.PaddleOCR.Models;
using Sdcb.PaddleOCR.Models.LocalV3;

namespace PaddleOCRSharp.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class OCRController : ControllerBase
    {
        /// <summary>
        /// Image to Base64 Code by OCR
        /// </summary>
        /// <param name="imageStr">Base64 Code</param>
        /// <returns></returns>
        [HttpPost("GetStringByBase64")]
        public string GetStringByBase64([FromForm] string imageStr)
        {
            FullOcrModel model = LocalFullModels.ChineseV3;

            byte[] sampleImageData = Convert.FromBase64String(imageStr.Substring(imageStr.IndexOf(",") + 1));

            using (PaddleOcrAll all = new PaddleOcrAll(model)
            {
                AllowRotateDetection = true, /* 允许识别有角度的文字 */
                Enable180Classification = false, /* 允许识别旋转角度大于90度的文字 */
            })
            // Load local file by following code:
            // using (Mat src2 = Cv2.ImRead(@"C:\test.jpg"))
            using (Mat src = Cv2.ImDecode(sampleImageData, ImreadModes.Color))
            {
                PaddleOcrResult result = all.Run(src);
                foreach (PaddleOcrResultRegion region in result.Regions)
                {
                    Console.WriteLine($"Text: {region.Text}, Score: {region.Score}, RectCenter: {region.Rect.Center}, RectSize:    {region.Rect.Size}, Angle: {region.Rect.Angle}");

                }
                return result.Text;
            }

        }

        /// <summary>
        /// Uploaded images are identified by OCR
        /// </summary>
        /// <param name="file">image</param>
        /// <returns></returns>
        [HttpPost("GetStringByFileImg")]
        public async Task<string> GetStringByFileImg(IFormFile file)
        {
            FullOcrModel model = LocalFullModels.ChineseV3;

            Stream stream = file.OpenReadStream();

            byte[] sampleImageData = new byte[stream.Length];

            await stream.ReadAsync(sampleImageData, 0, sampleImageData.Length);
            // 设置当前流的位置为流的开始 
            stream.Seek(0, SeekOrigin.Begin);
            using (PaddleOcrAll all = new PaddleOcrAll(model)
            {
                AllowRotateDetection = true, /* 允许识别有角度的文字 */
                Enable180Classification = false, /* 允许识别旋转角度大于90度的文字 */
            })

            using (Mat src = Cv2.ImDecode(sampleImageData, ImreadModes.Color))
            {
                PaddleOcrResult result = all.Run(src);
                Console.WriteLine("Detected all texts: \n" + result.Text);
                foreach (PaddleOcrResultRegion region in result.Regions)
                {
                    Console.WriteLine($"Text: {region.Text}, Score: {region.Score}, RectCenter: {region.Rect.Center}, RectSize:    {region.Rect.Size}, Angle: {region.Rect.Angle}");
                }
                return result.Text;
            }

        }
    }
}
